/*
 * Copyright 2005-2013 finalist.cn. All rights reserved.
 * Support: http://www.finalist.cn
 * License: http://www.finalist.cn/license
 */
package com.finalist.plugin.pay99billRMB;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.finalist.entity.Payment;
import com.finalist.entity.PluginConfig;
import com.finalist.plugin.PaymentPlugin;
import com.finalist.plugin.pay99billRMB.key.Pkipair;

/**
 * Plugin - 快钱支付--人民币网关
 * 
 * @author Finalist IT Group
 * @version 3.0
 */
@Component("pay99billRMBPlugin")
public class Pay99billRMBPlugin extends PaymentPlugin {

	@Override
	public String getName() {
		return "快钱支付[人民币网关]";
	}

	@Override
	public String getVersion() {
		return "1.0";
	}

	@Override
	public String getAuthor() {
		return "finalist";
	}

	@Override
	public String getSiteUrl() {
		return "http://www.finalist.cn";
	}

	@Override
	public String getInstallUrl() {
		return "pay_99billRMB/install.jhtml";
	}

	@Override
	public String getUninstallUrl() {
		return "pay_99billRMB/uninstall.jhtml";
	}

	@Override
	public String getSettingUrl() {
		return "pay_99billRMB/setting.jhtml";
	}

	@Override
	public String getRequestUrl() {
		return "https://www.99bill.com/gateway/recvMerchantInfoAction.htm";
	}

	@Override
	public RequestMethod getRequestMethod() {
		return RequestMethod.get;
	}

	@Override
	public String getRequestCharset() {
		return "UTF-8";
	}

	@Override
	public Map<String, Object> getParameterMap(String sn, String description, HttpServletRequest request) {
		PluginConfig pluginConfig = getPluginConfig();
		Payment payment = getPayment(sn);
		Map<String, Object> parameterMap = new LinkedHashMap<String, Object>();
		parameterMap.put("inputCharset", "1");
		parameterMap.put("pageUrl", getNotifyUrl(sn, NotifyMethod.sync));
		parameterMap.put("bgUrl", getNotifyUrl(sn, NotifyMethod.async));
		parameterMap.put("version", "v2.0");
		parameterMap.put("language", "1");
		parameterMap.put("signType", "4");
		parameterMap.put("merchantAcctId", pluginConfig.getAttribute("partner"));
		parameterMap.put("orderId", sn);
		parameterMap.put("orderAmount", payment.getAmount().multiply(new BigDecimal(100)).setScale(0).toString());
		parameterMap.put("orderTime", new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
		parameterMap.put("productName", StringUtils.abbreviate(description.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5 ]", ""), 100));
		parameterMap.put("productDesc", StringUtils.abbreviate(description.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5 ]", ""), 400));
		parameterMap.put("ext1", "shopxx");
		parameterMap.put("ext2", "shopxx");
		parameterMap.put("payType", "00");
		parameterMap.put("signMsg", generateSign(parameterMap));
		return parameterMap;
	}

	@Override
	public boolean verifyNotify(String sn, NotifyMethod notifyMethod, HttpServletRequest request) {
		PluginConfig pluginConfig = getPluginConfig();
		Payment payment = getPayment(sn);
		Map<String, Object> parameterMap = new LinkedHashMap<String, Object>();
		parameterMap.put("merchantAcctId", request.getParameter("merchantAcctId"));
		parameterMap.put("version", request.getParameter("version"));
		parameterMap.put("language", request.getParameter("language"));
		parameterMap.put("signType", request.getParameter("signType"));
		parameterMap.put("payType", request.getParameter("payType"));
		parameterMap.put("bankId", request.getParameter("bankId"));
		parameterMap.put("orderId", request.getParameter("orderId"));
		parameterMap.put("orderTime", request.getParameter("orderTime"));
		parameterMap.put("orderAmount", request.getParameter("orderAmount"));
		parameterMap.put("dealId", request.getParameter("dealId"));
		parameterMap.put("bankDealId", request.getParameter("bankDealId"));
		parameterMap.put("dealTime", request.getParameter("dealTime"));
		parameterMap.put("payAmount", request.getParameter("payAmount"));
		parameterMap.put("fee", request.getParameter("fee"));
		parameterMap.put("ext1", request.getParameter("ext1"));
		parameterMap.put("ext2", request.getParameter("ext2"));
		parameterMap.put("payResult", request.getParameter("payResult"));
		parameterMap.put("errCode", request.getParameter("errCode"));
		if (Pkipair.validateMsg(serilizeSign(parameterMap),request.getParameter("signMsg")) && pluginConfig.getAttribute("partner").equals(request.getParameter("merchantAcctId")) && sn.equals(request.getParameter("orderId")) && "10".equals(request.getParameter("payResult")) && payment.getAmount().multiply(new BigDecimal(100)).compareTo(new BigDecimal(request.getParameter("payAmount"))) == 0) {
			return true;
		}
		return false;
	}

	@Override
	public String getNotifyMessage(String sn, NotifyMethod notifyMethod, HttpServletRequest request) {
		if (notifyMethod == NotifyMethod.async) {
			/**
			 * 20141011
			 * 快钱现在启用异步回调方式
			 * pageUrl 只添加此属性才有启用
			 * bgUrl异步通知方式
			 */
//			return "<result>1</result> <redirecturl>" + getNotifyUrl(sn, NotifyMethod.async) + "</redirecturl>";
			return null;
		}
		return null;
	}

	@Override
	public Integer getTimeout() {
		return 21600;
	}

	/**
	 * 生成签名
	 * 
	 * @param parameterMap
	 *            参数
	 * @return 签名
	 */
	private String generateSign(Map<String, Object> parameterMap) {
		return Pkipair.signMsg(joinKeyValue(parameterMap, null, null, "&", true, "signMsg"));
	}
	
	private String serilizeSign(Map<String, Object> parameterMap) {
		return joinKeyValue(parameterMap, null, null, "&", true, "signMsg");
	}

}